package com.usvisatrack.core.factories;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.usvisatrack.core.common.Factory;
import com.usvisatrack.core.dao.model.SocialMedia;
import com.usvisatrack.core.dao.model.SocialMediaLog;
import com.usvisatrack.core.dao.model.Visa;
import com.usvisatrack.core.dao.model.VisaStatusLog;

/**
 * Item Data Factory
 * 
 * @author YuCheng Hu
 *
 */
public class LogFactory extends Factory {
	private static final int LISTING_PAGE_SIZE = 20;
	public static final int DEFAULT_AUTH_EXPIRATION = 10;

	private static Logger logger = LoggerFactory.getLogger(LogFactory.class);

	/**
	 * 
	 * @param id
	 * @return
	 */
	public static VisaStatusLog getVisaStatusLog(Long id) {
		logger.debug("Get Visa OBJ by ID - [{}]", id);
		Factory.beginTransaction();
		VisaStatusLog visaStatusLog = null;
		try {
			visaStatusLog = Factory.get(VisaStatusLog.class, id);
			Factory.commitTransaction();
		} catch (Exception ex) {
			visaStatusLog = null;
			logger.error("Get Visa by item ID error", ex);
		} finally {
			Factory.commitTransaction();
		}

		return visaStatusLog;
	}

	/**
	 * 
	 * @param dt
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<VisaStatusLog> getVisaStatusLogAfter(DateTime dt) {
		logger.debug("Get VisaStatusLog OBJ After - [{}]", dt);
		Factory.beginTransaction();
		try {
			Criteria criteria = Factory.createCriteria(VisaStatusLog.class);
			criteria.add(Restrictions.ge("createDate", dt.toDate()));
			criteria.setMaxResults(256);
			return criteria.list();
		} catch (Exception ex) {
			logger.error("Get VisaStatusLog List by Date Range ERROR", ex);
		} finally {
			Factory.rollbackTransaction();
		}
		return null;
	}

	public static SocialMediaLog getSocialMediaLog(SocialMedia socialMedia, VisaStatusLog visaStatusLog) {
		logger.debug("Get Visa SocialMedia Log");
		Factory.beginTransaction();
		SocialMediaLog socialMediaLog = null;
		try {
			Criteria criteria = Factory.createCriteria(SocialMediaLog.class);
			criteria.add(Restrictions.eq("socialMedia", socialMedia));
			criteria.add(Restrictions.eq("visaStatusLog", visaStatusLog));
			socialMediaLog = (SocialMediaLog) criteria.uniqueResult();
		} catch (Exception ex) {
			visaStatusLog = null;
			logger.error("Get Visa by item ID error", ex);
		} finally {
			Factory.commitTransaction();
		}
		return socialMediaLog;
	}

}